<template>
  <div>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-form :model="queryInfo" ref="queryInfo">
        <el-col>
          <el-input
            class="search"
            placeholder="搜索"
            v-model="queryInfo.keyword"
            clearable
            @clear="getUserList"
          />
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="getUserList"
            >搜索</el-button
          >
          <el-button type="primary" size="small" @click="resetFormData"
            >重置</el-button
          >
        </el-col>
      </el-form>
      <!-- 新增用户 -->
      <el-row class="right">
        <el-button
          type="primary"
          size="small"
          class="right-button"
          @click="AddDialogVisible = true"
        >
          <i class="el-icon-edit"></i>
          <span>新增用户</span>
        </el-button>
      </el-row>
      <el-row class="res">
        <el-col class="el-icon-info"
          ><span>共 {{ this.total }} 条数据</span></el-col
        >
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="UserList" border style="width: 100%">
        <el-table-column label="序号" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="邮箱" width="180" align="center"
          ><template slot-scope="scope">
            <span>{{ scope.row.email }}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系电话" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.phone }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户名" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column label="权限组名称" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.permission_group_title }}</span>
          </template>
        </el-table-column>
        <el-table-column label="角色" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.role }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 新增用户对话框 -->
    <el-dialog
      title="创建用户"
      :visible.sync="AddDialogVisible"
      width="50%"
      @close="resetForm"
    >
      <!-- 内容区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
      >
        <el-form-item label="用户名" prop="userame">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="addForm.role"></el-input>
        </el-form-item>
        <el-form-item label="权限组名称" prop="permission_group_id">
          <el-select v-model="addForm.permission_group_id" placeholder="请选择">
            <el-option
              v-for="item in PermissionGroupsList"
              :value="item.id"
              :key="item.key"
              :label="item.permission_group_title"
            ></el-option>
            <!-- <el-option label="超级管理员组"></el-option>
            <el-option label="编辑组"></el-option> -->
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="介绍" prop="introduction">
          <el-input
            type="textarea"
            v-model="textarea"
            placeholder="please input"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="AddDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑用户对话框 -->
    <el-dialog title="编辑用户" :visible.sync="editDialogVisible" width="50%">
      <!-- 内容区域 -->
      <el-form
        :model="editForm"
        :rules="addFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="editForm.role"></el-input>
        </el-form-item>
        <el-form-item label="权限组名称" prop="permission_group_id">
          <el-select
            v-model="editForm.permission_group_id"
            placeholder="请选择"
          >
            <el-option
              v-for="(item, index) in PermissionGroupsList"
              :value="item.id"
              :key="index"
              :label="item.permission_group_title"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="介绍" prop="introduction">
          <el-input
            type="textarea"
            v-model="textarea"
            placeholder="please input"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo(editForm.id)"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { UserData, AddUser, deleteUser, editUser } from '@/api/user'
export default {
  data() {
    return {
      // 前往页数
      // currentPage4: 1,
      queryInfo: {
        keyword: '',
        // 当前页数
        page: 1,
        // 页码
        pagesize: 10
      },
      // 列表数据
      UserList: [],
      // 总条数
      total: 0,
      textarea: '',
      // 新增用户对话框的显示和隐藏
      AddDialogVisible: false,
      // 编辑用户对话框的显示与隐藏
      editDialogVisible: false,
      addForm: {
        email: '',
        phone: '',
        username: '',
        role: '',
        password: '',
        permission_group_id: '',
        introduction: ''
      },
      editForm: [],
      // 权限分组列表
      PermissionGroupsList: [],
      // 表单预验证规则
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '用户名的长度在3~10个字符之间',
            trigger: 'blur'
          }
        ],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '用户名的长度在6~15个字符之间',
            trigger: 'blur'
          }
        ],
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    // 获取列表信息
    async getUserList() {
      try {
        const { data } = await UserData(this.queryInfo)
        console.log(data)
        this.UserList = data.list
        this.total = data.counts
        this.PermissionGroupsList = data.list
      } catch (err) {
        this.$message.error('获取数据失败！' + err)
      }
    },
    // 编辑用户对话框
    async handleEdit(row) {
      console.log(row)
      this.editForm = row
      this.editDialogVisible = true
    },
    // 编辑数据
    editUserInfo(id) {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        console.log(id)
        const form = {
          avatar: this.editForm.avatar,
          email: this.editForm.email,
          id: this.editForm.id,
          introduction: this.editForm.phointroductionne,
          permission_group_id: this.editForm.permission_group_id,
          phone: this.editForm.phone,
          role: this.editForm.role,
          username: this.editForm.username
        }
        try {
          const { data } = await editUser(form)
          console.log(data)
        } catch (err) {
          this.$message.error('编辑失败，请重试')
        }
        this.editDialogVisible = false
        this.getUserList()
        this.$message.success('编辑成功')
      })
    },
    // 删除
    async handleDelete(id) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data } = await deleteUser(id)
      console.log(data)
      this.$message.success('删除成功')
    },
    // 监听 pagesize 变化事件
    handleSizeChange(newSize) {
      this.queryInfo.page = newSize
      this.getUserList()
    },
    // 监听页码值改变事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagesize = newPage
      this.getUserList()
    },
    // 监听修改对话框关闭重置事件
    resetForm() {
      this.$refs.addFormRef.resetFields()
    },
    // 重置
    resetFormData() {
      this.$refs.queryInfo.resetFields()
    },
    // 添加表单用户
    addUser() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        try {
          const { data } = await AddUser(this.addForm)
          console.log(data)
        } catch (err) {
          this.$message.error('添加数据失败！' + err)
        }
        // 关闭对话框
        this.AddDialogVisible = false
        // 重新渲染数据
        this.getUserList()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.box-card {
  /deep/ .search {
    width: 200px;
    height: 36px;
  }
  .res {
    height: 33px;
    display: flex;
    align-items: center;
    background-color: #f4f4f5;
    padding-left: 20px;
    margin-bottom: 10px;
    color: #909399;
    font-size: 13px;
    span {
      padding-left: 8px;
    }
  }
  .right {
    display: flex;
    justify-content: flex-end;
    .right-button {
      display: block;
      margin: 40px 0 10px 0;
    }
  }
}
.el-dialog {
  .el-form {
    width: 400px;
    margin-left: 120px;
  }
}
/deep/.el-dialog__header {
  background-color: #409eff;
  .el-dialog__title {
    font-size: 18px;
    color: #fff;
  }
  .el-icon-close {
    color: #fff;
  }
}
/deep/.el-dialog__footer {
  text-align: center;
}
</style>
